package org.jeecg.modules.job.api.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.modules.job.rms.entity.RmsCourse;
import org.jeecg.modules.job.rms.entity.RmsCourseApply;
import org.jeecg.modules.job.rms.service.IRmsCourseApplyService;
import org.jeecg.modules.job.rms.service.IRmsCourseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 课程管理相关接口
 */
@RestController
@RequestMapping("/api/course")
@Api(tags="移动端课程管理模块")
@Slf4j
public class CourseController{

    @Autowired
    private IRmsCourseService courseService;
    @Autowired
    private IRmsCourseApplyService courseApplyService;

    /**
     * 查询课程列表
     */
    @ResponseBody
    @RequestMapping(value = "/getCourseList",method = RequestMethod.GET)
    @ApiOperation(value = "查询课程列表", notes = "查询课程列表")
    public Result<Object> getCourseList(RmsCourse param,
                                        @RequestParam(name="page", defaultValue="1") Integer page,
                                        @RequestParam(name="limit", defaultValue="10") Integer limit
    ) {
        IPage<RmsCourse> pageInfo= courseService.page(new Page<>(page,limit),new QueryWrapper<>(param));
        return Result.OK(pageInfo);
    }

    /**
     * 查询课程信息
     */
    @ResponseBody
    @RequestMapping(value = "/getCourseDetail",method = RequestMethod.GET)
    @ApiOperation(value = "查询课程信息", notes = "查询课程信息")
    public Result<Object> getCompanyDetail(String id) {
        RmsCourse result=courseService.getById(id);
        try{
            //查看是否已报名
            RmsCourseApply param=new RmsCourseApply();
            LoginUser user = (LoginUser) SecurityUtils.getSubject().getPrincipal();
            param.setMemberId(user.getId());
            param.setCourseId(id);
            RmsCourseApply ar=courseApplyService.getOne(new QueryWrapper<>(param));
            if (ar!=null){
                result.setIfApply(true);
            }else{
                result.setIfApply(false);
            }
        }catch (Exception e){
           System.err.println( e.getMessage());
        }
        return Result.OK(result);
    }

    /**
     * 课程报名
     */
    @ResponseBody
    @RequestMapping(value = "/applyCourse",method = RequestMethod.POST)
    @ApiOperation(value = "课程报名", notes = "课程报名")
    public Result<Object> applyCourse(@RequestBody RmsCourseApply param) {
        //取得当前Token登录者会员的ID
        try {
            LoginUser user = (LoginUser) SecurityUtils.getSubject().getPrincipal();
            param.setMemberId(user.getId());
            courseApplyService.add(param);
            return Result.OK();
        }catch (Exception e){
            return Result.error(e.getMessage());
        }
    }



    /**
     * 查询我的课程列表
     */
    @ResponseBody
    @RequestMapping(value = "/getApplyCourseList",method = RequestMethod.GET)
    @ApiOperation(value = "查询我的课程列表", notes = "查询我的课程列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "页码", dataType = "Integer", paramType = "query"),
            @ApiImplicitParam(name = "limit", value = "每页数量", dataType = "Integer", paramType = "query")
    })
    public Result<Object> getApplyCourseList(RmsCourseApply param,
                                             @RequestParam(name="page", defaultValue="1") Integer page,
                                             @RequestParam(name="limit", defaultValue="10") Integer limit) {
        try {
            LoginUser user = (LoginUser) SecurityUtils.getSubject().getPrincipal();
            param.setMemberId(user.getId());
            IPage<RmsCourseApply> pageInfo= courseApplyService.findPageBySpec(new Page<>(page,limit),param);
            return Result.OK(pageInfo);
        }catch (Exception e){
            return Result.error(e.getMessage());
        }
    }

}



